<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getCurrentUser, updateUserInfo, updatePassword } from '@/api/user'

const userForm = ref({
  id: '',
  username: '',
  name: '',
  mobile: '',
  email: '',
  graduateYear: '',
  majorCode: '',
  workUnit: '',
  industry: ''
})

const passwordForm = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const userFormRef = ref(null)
const passwordFormRef = ref(null)
const loading = ref(false)
const activeTab = ref('info')

// 表单校验规则
const userRules = {
  name: [{ required: true, message: '请输入真实姓名', trigger: 'blur' }],
  mobile: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  graduateYear: [{ required: true, message: '请选择毕业年份', trigger: 'change' }]
}

const passwordRules = {
  oldPassword: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.value.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 获取用户信息
const getUserInfo = async () => {
  loading.value = true
  try {
    const res = await getCurrentUser()
    if (res.code === 200) {
      Object.assign(userForm.value, res.data)
    } else {
      ElMessage.error(res.msg || '获取用户信息失败')
    }
  } catch (error) {
    ElMessage.error(error.response?.data?.msg || '获取用户信息失败')
  } finally {
    loading.value = false
  }
}

// 更新用户信息
const handleUpdateInfo = async () => {
  if (!userFormRef.value) return
  await userFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res = await updateUserInfo(userForm.value)
        if (res.code === 200) {
          ElMessage.success('个人信息更新成功')
        } else {
          ElMessage.error(res.msg || '更新失败')
        }
      } catch (error) {
        ElMessage.error(error.response?.data?.msg || '更新失败')
      } finally {
        loading.value = false
      }
    }
  })
}

// 更新密码
const handleUpdatePassword = async () => {
  if (!passwordFormRef.value) return
  await passwordFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res = await updatePassword({
          oldPassword: passwordForm.value.oldPassword,
          newPassword: passwordForm.value.newPassword
        })
        if (res.code === 200) {
          ElMessage.success('密码修改成功')
          passwordForm.value = {
            oldPassword: '',
            newPassword: '',
            confirmPassword: ''
          }
        } else {
          ElMessage.error(res.msg || '修改失败')
        }
      } catch (error) {
        ElMessage.error(error.response?.data?.msg || '修改失败')
      } finally {
        loading.value = false
      }
    }
  })
}

onMounted(() => {
  getUserInfo()
})
</script>

<template>
  <div class="profile-container">
    <el-card v-loading="loading">
      <el-tabs v-model="activeTab">
        <!-- 个人信息表单 -->
        <el-tab-pane label="个人信息" name="info">
          <el-form
            ref="userFormRef"
            :model="userForm"
            :rules="userRules"
            label-width="100px"
          >
            <el-form-item label="用户名">
              <el-input v-model="userForm.username" disabled />
            </el-form-item>
            
            <el-form-item label="真实姓名" prop="name">
              <el-input v-model="userForm.name" />
            </el-form-item>
            
            <el-form-item label="手机号码" prop="mobile">
              <el-input v-model="userForm.mobile" />
            </el-form-item>
            
            <el-form-item label="邮箱地址" prop="email">
              <el-input v-model="userForm.email" />
            </el-form-item>
            
            <el-form-item label="毕业年份" prop="graduateYear">
              <el-date-picker
                v-model="userForm.graduateYear"
                type="year"
                placeholder="选择毕业年份"
              />
            </el-form-item>
            
            <el-form-item label="专业" prop="majorCode">
              <el-input v-model="userForm.majorCode" />
            </el-form-item>
            
            <el-form-item label="公司" prop="workUnit">
              <el-input v-model="userForm.workUnit" />
            </el-form-item>
            
            <el-form-item label="行业" prop="industry">
              <el-input v-model="userForm.industry" />
            </el-form-item>
            
            <el-form-item>
              <el-button type="primary" @click="handleUpdateInfo">保存修改</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        
        <!-- 修改密码表单 -->
        <el-tab-pane label="修改密码" name="password">
          <el-form
            ref="passwordFormRef"
            :model="passwordForm"
            :rules="passwordRules"
            label-width="100px"
          >
            <el-form-item label="原密码" prop="oldPassword">
              <el-input
                v-model="passwordForm.oldPassword"
                type="password"
                show-password
              />
            </el-form-item>
            
            <el-form-item label="新密码" prop="newPassword">
              <el-input
                v-model="passwordForm.newPassword"
                type="password"
                show-password
              />
            </el-form-item>
            
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input
                v-model="passwordForm.confirmPassword"
                type="password"
                show-password
              />
            </el-form-item>
            
            <el-form-item>
              <el-button type="primary" @click="handleUpdatePassword">
                修改密码
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<style scoped>
.profile-container {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

:deep(.el-input),
:deep(.el-date-picker) {
  width: 100%;
  max-width: 400px;
}
</style> 